package com.zeng.bean.restmodel;

import com.zeng.annotation.restmodel.RestModel;
import com.zeng.pojo.restmodel.BaseRestModel;
import lombok.Data;

import java.util.List;

/**
 * 用于获取表信息
 */
@Data
public class RestTableInfo {
    private String tableName;

    private List<RestTableFieldInfo> fields;

    private Class<? extends BaseRestModel> clazz;

    public RestTableInfo(RestModelInfo modelInfo){
        clazz = modelInfo.getClazz();
        tableName = modelInfo.getTableName();
        fields = RestTableFieldInfo.loadByTableClass(clazz);
    }

    public static RestTableInfo load(RestModelInfo modelInfo){
        return new RestTableInfo(modelInfo);
    }

    /**
     * 是否存在字段结构差异
     */
    public boolean hasDifference(List<RestTableFieldInfo> fds){
        if (fds == fields) return true;
        if (fds == null) return false;
        if (fds.size() != fields.size()) return false;

        for (RestTableFieldInfo fd : fds) {
            if(fds.stream().noneMatch(f -> f.equals(fd))) return false;
        }

        return true;
    }
}

